﻿Public Class frmCabinets
    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing

    Private Sub frmCabinets_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        TE_NOM.Clear()
        InitListeCABINETS(0)
    End Sub

    Private Sub InitListeCABINETS(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_CABINETS.Items.Clear()

        ' affiche les 50 CABINETS à partir de l'élève courant
        szSQL = "CALL spGet_CABINETbyNOM('" & gSocieteCode & "','%" & TE_NOM.Text & "%')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_CABINETS.Items.Add(rs.Fields("code").Value)
            LV_CABINETS.Items(nb).SubItems.Add(rs.Fields("nom").Value)
            LV_CABINETS.Items(nb).SubItems.Add(nv(rs.Fields("tel1").Value))
            LV_CABINETS.Items(nb).SubItems.Add(nv(rs.Fields("tel2").Value))
            LV_CABINETS.Items(nb).SubItems.Add(nv(rs.Fields("telp").Value))
            LV_CABINETS.Items(nb).SubItems.Add(nv(rs.Fields("ville").Value))
            If rs.Fields("afficher").Value = 0 Then
                LV_CABINETS.Items(nb).ForeColor = Color.Gray
            End If
            nb = nb + 1
            ' Get the next record
            rs.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        LB_NbLignes.Text = CStr(nb) & " fiche(s)"
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_CABINETS)

    End Sub

    Private Sub LV_CABINETS_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_CABINETS.DoubleClick
        If LV_CABINETS.SelectedItems.Count > 0 Then
            ' afficher le Cabinet 
            frmCabinetMAJ.pCode_cab = LV_CABINETS.SelectedItems(0).Text
            frmCabinetMAJ.ShowDialog()
            ' réaffiche la viewlist
            InitListeCABINETS(LV_CABINETS.SelectedItems(0).Index)
        End If
    End Sub

    Private Sub LV_CABINETS_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_CABINETS.KeyDown
        Select Case e.KeyCode
            Case Keys.Enter         ' Enter = DoubleClick
                LV_CABINETS_DoubleClick(sender, e)

            Case Keys.Insert        ' Insert = ' Ajouter un élève
                BO_ADD_LIGNE_Click(sender, e)
        End Select
    End Sub

    Private Sub BO_ADD_LIGNE_Click(sender As System.Object, e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' création cabinet
        frmCabinetMAJ.pCode_cab = 0
        frmCabinetMAJ.ShowDialog()
        InitListeCABINETS(0)
    End Sub

    Private Sub TE_NOM_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TE_NOM.KeyDown
        If e.KeyCode = Keys.Enter Then
            ' affiche la listview
            InitListeCABINETS(0)
        End If
    End Sub

End Class